package com.uex.dev.manage.admin.controller;

import com.google.common.base.Preconditions;
import com.uex.dev.manage.admin.enums.DeviceTypeEnum;
import com.uex.dev.manage.admin.po.DeviceImgPO;
import com.uex.dev.manage.admin.po.DeviceModelPO;
import com.uex.dev.manage.admin.service.IDeviceImgService;
import com.uex.dev.manage.admin.service.IDeviceModelService;
import com.uex.dev.manage.admin.service.IModelRobotService;
import com.uex.dev.manage.admin.service.IModelUhubService;
import com.uex.dev.manage.admin.vo.device.*;
import com.uex.dev.manage.common.base.ModelResult;
import io.micrometer.common.util.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author lzs
 * @create 2024/04/11
 * @description
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Tag(name = "设备型号管理")
@RequestMapping("/model")
@RestController
public class DeviceModelController {

    @Resource
    private IDeviceModelService deviceModelService;

    @Resource
    private IModelRobotService modelRobotService;

    @Resource
    private IModelUhubService modelUhubService;

    @Resource
    private IDeviceImgService deviceImgService;

    @Operation(description = "获取设备型号列表")
    @GetMapping("/list")
    public ModelResult<List<DeviceModelResp>> selectList(String deviceType) {
        return new ModelResult(deviceModelService.selectList(deviceType));
    }

    @Operation(description = "添加设备型号")
    @PostMapping("/add")
    public ModelResult<?> addDeviceModel(@RequestBody DeviceModelPO req) {
        Preconditions.checkArgument(Objects.nonNull(req.getDeviceType()), "设备类型不能为空");
        Preconditions.checkArgument(Strings.isNotEmpty(req.getDeviceModel()), "设备型号不能为空");
        return new ModelResult<>(deviceModelService.addDeviceModel(req));
    }

    @Operation(description = "删除设备型号")
    @DeleteMapping("/del")
    public ModelResult<?> delDeviceModel(@RequestParam Integer modelId) {
        Preconditions.checkArgument(Objects.nonNull(modelId), "设备型号Id不能为空");
        return deviceModelService.delDeviceModel(modelId) ? ModelResult.SUCCESS_VOID_RESULT : ModelResult.SYSTEM_ERROR_RESULT;
    }

    @Operation(description = "获取指定型号的图像列表")
    @GetMapping("/imgList")
    public ModelResult<List<DeviceImgResp>> selectList(@RequestParam DeviceTypeEnum deviceType, @RequestParam String deviceModel) {
        Preconditions.checkArgument(StringUtils.isNotBlank(deviceModel), "设备型号不能为空");
        return new ModelResult<>(deviceImgService.selectList(deviceType, deviceModel));
    }

    @Operation(description = "添加指定型号图片")
    @PostMapping("/imgAdd")
    public ModelResult<?> addDeviceImg(@RequestBody DeviceImgPO deviceImgAddReq) {
        Preconditions.checkArgument(StringUtils.isNotBlank(deviceImgAddReq.getDeviceModel()), "设备型号不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(deviceImgAddReq.getImgName()), "图片名称不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(deviceImgAddReq.getImgUrl()), "图片URL不能为空");
        return deviceImgService.addDeviceImg(deviceImgAddReq) ? ModelResult.SUCCESS_VOID_RESULT : ModelResult.SYSTEM_ERROR_RESULT;
    }

    @Operation(description = "删除指定型号图片")
    @DeleteMapping("/imgDel")
    public ModelResult<?> delDeviceImg(@RequestParam Integer imgId) {
        Preconditions.checkArgument(Objects.nonNull(imgId), "图片Id不能为空");
        return deviceImgService.delDeviceImg(imgId) ? ModelResult.SUCCESS_VOID_RESULT : ModelResult.SYSTEM_ERROR_RESULT;
    }

    @Operation(description = "获取收单机型号的默认能力")
    @GetMapping("/robotFeature")
    public ModelResult<RobotInfo> getRobotFeature(@RequestParam Integer modelId) {
        Preconditions.checkArgument(Objects.nonNull(modelId), "设备型号Id不能为空");
        return new ModelResult<>(modelRobotService.getRobotFeature(modelId));
    }

    @Operation(description = "修改收单机型号的默认能力")
    @PostMapping("/robotFeature")
    public ModelResult<?> updateRobotFeature(@RequestBody RobotInfo robotModelUpdateReq) {
        Preconditions.checkArgument(Objects.nonNull(robotModelUpdateReq.getModelId()));
        modelRobotService.updateRobotFeature(robotModelUpdateReq);
        return ModelResult.SUCCESS_VOID_RESULT;
    }

    @Operation(description = "获取云盾型号的默认能力")
    @GetMapping("/uhubFeature")
    public ModelResult<UhubInfo> getUhubFeature(@RequestParam Integer modelId) {
        Preconditions.checkArgument(Objects.nonNull(modelId), "设备型号Id不能为空");
        return new ModelResult<>(modelUhubService.getUhubFeature(modelId));
    }

    @Operation(description = "修改云盾型号的默认能力")
    @PostMapping("/uhubFeature")
    public ModelResult<?> updateUhubFeature(@RequestBody UhubInfo uhubModelUpdateReq) {
        Preconditions.checkArgument(Objects.nonNull(uhubModelUpdateReq.getModelId()));
        modelUhubService.updateUhubFeature(uhubModelUpdateReq);
        return ModelResult.SUCCESS_VOID_RESULT;
    }

    @Operation(description = "获取云盾设备能力列表")
    @GetMapping ("/uhubFeatureList")
    public ModelResult<?> getUhubFeatureList(){
        return new ModelResult<>(modelUhubService.getUhubFeatureList());
    }
}

